﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Security.Cryptography;
using System.IO;
using System.Reflection;

using SB.Logging;
using SB.Database;
using SB.UserSettings;
using SB.Tools;
using SB.MediaLibrary.Entities;
using System.Xml;
using System.Collections;

namespace SB.MediaLibrary
{
    public class EntityManager :
        IDatabaseTasks,
        IFilm,
        IGenre,
        IMedium
    {
        private static readonly ILogger log = LoggingFacade.GetLogger(typeof(EntityManager));

        public EntityManager()
        {    
            Assembly assembly = Assembly.GetExecutingAssembly();
            string loggingConfigResourceName = "SB.MediaLibrary.Config.Log4Net.config";
            string databaseConfigResourceName = "SB.MediaLibrary.Config.NHibernate.config";

            LoggingFacade.Initialize(assembly, loggingConfigResourceName);
            DatabaseFacade.Initialize(assembly, databaseConfigResourceName);

            //DatabaseFacade.DataAccessInterface.CreateTables();
            //DoInsertTestData();

            DatabaseFacade.DataAccessInterface.OpenConnection();
            UserSettingsFacade.XMLInterface.Initialize("MediaLibrary");
        }

        #region Film Tasks

        public IList<Film> FilmSuchen(Film film)
        {
            return DatabaseFacade.DataAccessInterface.FindMatching<Film>(film, DatabaseFacade.MatchMode.Anywhere);
        }

        #endregion


        #region Medium Tasks

        public IList<Medium> MediumSuchen(Medium medium)
        {
            return DatabaseFacade.DataAccessInterface.FindMatching<Medium>(medium, DatabaseFacade.MatchMode.Anywhere);
        }

        #endregion


        #region Genre Tasks

        public IList<Genre> GenreSuchen(Genre genre)
        {
            return DatabaseFacade.DataAccessInterface.FindMatching<Genre>(genre, DatabaseFacade.MatchMode.Anywhere);
        }

        public IList<Genre> GenreAlleLaden()
        {
            return DatabaseFacade.DataAccessInterface.GetAll<Genre>();
        }

        public Genre GenreSpeichern(Genre genre)
        {
            DatabaseFacade.DataAccessInterface.Save(genre);
            DatabaseFacade.DataAccessInterface.Flush();
            return genre;
        }

        public void GenreEntfernen(Genre genre)
        {
            DatabaseFacade.DataAccessInterface.Delete(genre);
            DatabaseFacade.DataAccessInterface.Flush();
        }

        #endregion


        #region Database Tasks

        public void DoInsertTestData()
        {
            InsertTestData.Execute();
        }

        public void EmptyTables()
        {
            DatabaseFacade.DataAccessInterface.EmptyTables();
        }

        public void CreateTables()
        {
            DatabaseFacade.DataAccessInterface.CreateTables();
        }

        #endregion
    }
}
